package top.amot.forceview.force

class ForceCenter : AbstractForce {
    private var x = 0.0f
    private var y = 0.0f

    constructor() {}
    constructor(x: Float, y: Float) {
        this.x = x
        this.y = y
    }

    override fun apply(alpha: Float) {
        var sx = 0.0f
        var sy = 0.0f
        val n = nodes.size
        for (node in nodes) {
            sx += node.x
            sy += node.y
        }
        sx = sx / n - x
        sy = sy / n - y
        for (node in nodes) {
            node.x -= sx
            node.y -= sy
        }
    }

    fun x(x: Float): ForceCenter {
        this.x = x
        return this
    }

    fun y(y: Float): ForceCenter {
        this.y = y
        return this
    }

    companion object {
        const val NAME = "Center"
    }
}